<?php
##############################################
#
#   我的关注
# @ Time : 2017-2-28 - 下午11:39:25
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Model;

class CollectModel extends IndexBaseModel{

	/*活动订单表  */
	protected $tableName = 'collect';
	
	
	/**
	 * 提交订单
	 */
	public function addOrder($data) {
	    return $this->add($data);
	}
	
	/**
	  * 统计订单
	  * @param int $activity_id
	  * @param int $user_id
	  * @date 2017年8月7日 - 下午2:26:43
	  * @author JiangYongWei
	 */
	public function order_count($user_id) {
	    $map = "user_id = $user_id";
        return  array(
	        $this->where($map." and order_status = 1 and payment_status = 1")->count(),
	        $this->where($map." and order_status = 2 and payment_status = 2")->count(),
	        $this->where($map." and order_status = 3 and payment_status = 2")->count(),
	        $this->where($map." and order_status = 4")->count()
	    );
	}
	
	/**
	  * 我关注的活动
	  * @param int $user_id
	  * @date 2017年9月1日 - 上午10:56:21
	  * @author JiangYongWei
	 */
	public function collectActivity($user_id) {
	    $field = array('co.id,co.activity_id', 'ac.activity_name',
	             'ac.sponsor', 'ac.area', 'ac.activity_img', 'ac.start_time', 'ac.end_time'
	    );
	    $map = array('co.user_id'=>$user_id);// 'end_time'=>array('egt', time())
	    $count = $this->alias('co')->join("left join __ACTIVITY__ ac on ac.id = co.activity_id")->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $this->alias('co')
	                   ->field($field)
	                   ->join("left join __ACTIVITY__ ac on ac.id = co.activity_id")
	                   ->where($map)
	                   ->order('co.id desc')
	                   ->limit($Page->firstRow.','.$Page->listRows)
	                   ->select();
	    return array($result, $show, $count);
	}
	
	/**
	  * 取消收藏
	  * @param int $collect_id
	  * @date 2017年9月4日 - 上午11:46:37
	  * @author JiangYongWei
	 */
	public function cancelCollect($collect_id){
	    return $this->where("id = $collect_id")->delete();
	}
	
	/**
	 * 待付款订单
	 * @param int $user_id
	 * @date 2017年9月1日 - 上午10:56:21
	 * @author JiangYongWei
	 */
	public function stayOrder($user_id) {
	    $field = array('r.id', 'r.order_sn', 'r.activity_id', 'r.activity_name', 'r.amount',
	        'r.order_status', 'r.payment_status', 'ac.sponsor', 'ac.area', 'ac.activity_img', 'ac.signup_number'
	    );
	    $map = array('r.user_id'=>$user_id, 'r.order_status'=>1);// 'end_time'=>array('egt', time())
	    $count = $this->alias('r')->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $this->alias('r')
                	    ->field($field)
                	    ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
                	    ->where($map)
                	    ->order('id desc')
                	    ->limit($Page->firstRow.','.$Page->listRows)
                	    ->select();
	    return array($result, $show);
	}
	
	/**
	 * 待参与订单
	 * @param int $user_id
	 * @date 2017年9月1日 - 上午10:56:21
	 * @author JiangYongWei
	 */
	public function stayParticipateOrder($user_id) {
	    $field = array('r.id', 'r.order_sn', 'r.activity_id', 'r.activity_name', 'r.amount',
	        'r.order_status', 'r.payment_status', 'ac.sponsor', 'ac.area', 'ac.activity_img', 'ac.signup_number'
	    );
	    $map = array('r.user_id'=>$user_id, 'r.order_status'=>2);// 'end_time'=>array('egt', time())
	    $count = $this->alias('r')->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->alias('r')
                	    ->field($field)
                	    ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
                	    ->where($map)
                	    ->order('id desc')
                	    ->limit($Page->firstRow.','.$Page->listRows)
                	    ->select();
	    return array($result, $show);
	}
	
	/**
	 * 已完成订单
	 * @param int $user_id
	 * @date 2017年9月1日 - 上午10:56:21
	 * @author JiangYongWei
	 */
	public function completeOrder($user_id) {
	    $field = array('r.id', 'r.order_sn', 'r.activity_id', 'r.activity_name', 'r.amount',
	        'r.order_status', 'r.payment_status', 'ac.sponsor', 'ac.area', 'ac.activity_img', 'ac.signup_number'
	    );
	    $map = array('r.user_id'=>$user_id, 'r.order_status'=>3);// 'end_time'=>array('egt', time())
	    $count = $this->alias('r')->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->alias('r')
                	    ->field($field)
                	    ->join("left join __ACTIVITY__ ac on ac.id = r.activity_id")
                	    ->where($map)
                	    ->order('id desc')
                	    ->limit($Page->firstRow.','.$Page->listRows)
                	    ->select();
	    return array($result, $show);
	}
}
